/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons.impl;

import org.dctmcommons.EmptyStringParameterException;
import org.dctmcommons.IParamCheck;
import org.dctmcommons.IStringHelper;
import org.dctmcommons.NullParameterException;

/**
 * Default implementation of {@link IParamCheck}.
 * @author Mark Davidson
 */
public class ParamCheck implements IParamCheck {
	
	/** Error message for missing string parameter. */
	private static final String MSG_NULL_STRING =
								"Mandatory string parameter is null or empty";
	
	/** Error message for missing parameter. */
	private static final String MSG_NULL_PARAM =
								"Mandatory parameter is null";
	/** The string helper instance. */
	private static final IStringHelper stringHelper =
													StringHelper.getInstance();
	
	/**
	 * Default constructor.
	 */
	private ParamCheck() {}
	
	/**
	 * Factory method.
	 * @return A new instance of {@link IParamCheck}.
	 */
	public static IParamCheck getInstance() {
		return new ParamCheck();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.IParamCheck#checkObject(java.lang.Object)
	 */
	@Override
	public void checkObject(final Object value) throws NullParameterException {
		checkObject(value, null);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.IParamCheck#checkObject(java.lang.Object, java.lang.String)
	 */
	@Override
	public void checkObject(final Object value, final String name)
		throws NullParameterException {
		if (value == null) {
			final String suffix = (name == null) ? "" : ": " + name;
			throw new NullParameterException(MSG_NULL_PARAM + suffix);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.IParamCheck#checkString(java.lang.String)
	 */
	@Override
	public void checkString(final String value) throws EmptyStringParameterException {
		checkString(value, null);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.IParamCheck#checkString(java.lang.String, java.lang.String)
	 */
	@Override
	public void checkString(final String value, final String name)
		throws EmptyStringParameterException {
		if (stringHelper.isEmpty(value)) {
			final String suffix = (stringHelper.isEmpty(name))
															? "" :
															": " + name;
			throw new EmptyStringParameterException(MSG_NULL_STRING + suffix);
		}
	}
}
